package com.daguchuangyi.erp_android.productlist;

import com.daguchuangyi.erp_android.manager.database.ProductSelectInfo;
import com.daguchuangyi.erp_android.manager.database.ProductSelectInfo_Table;
import com.daguchuangyi.erp_android.model.product.Product;
import com.jude.utils.JUtils;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by lijianjian on 2018/3/9.
 */

public class ProductSummaryManager {
    private static final String TAG = "ProductSummaryManager";

    public static final int STYLE_LIST = 0x1;
    public static final int STYLE_GRID = 0x2;

    private static ProductSummaryManager sInstance;

    private int mCurrentState = STYLE_LIST;

    private Set<Integer> mCheckedProudctIdList;

    private Set<OnProductCheckChangeListenner> mOnProductCheckChangeListenners = new HashSet<>();

    public static synchronized ProductSummaryManager getInstance() {
        if (sInstance == null) {
            synchronized (ProductSummaryManager.class) {
                sInstance = new ProductSummaryManager();
            }
        }
        return sInstance;
    }

    private ProductSummaryManager() {
        mCheckedProudctIdList = new HashSet<>();
        List<ProductSelectInfo> selectUserIds = SQLite.select().from(ProductSelectInfo.class).queryList();
        for (ProductSelectInfo item : selectUserIds) {
            mCheckedProudctIdList.add(item.id);
        }
    }

    public void setCurrentState(int currentState) {
        mCurrentState = currentState;
    }

    public int getCurrentState() {
        return mCurrentState;
    }

    public Set<Integer> getCheckedProudctIdList() {
        return mCheckedProudctIdList;
    }

    public synchronized void addProductToCheck(Product product) {
        addProductToCheck(product, true);
    }

    public synchronized void addProductToCheck(Product product, boolean needNotify) {
        if (product != null) {
            mCheckedProudctIdList.add(product.getId());
            ProductSelectInfo item = new ProductSelectInfo();
            item.id = product.getId();
            item.save();
            if (needNotify) {
                notifyCheckChanged();
            }
        }
    }

    public synchronized void removeProductChecked(Product product) {
        removeProductChecked(product, true);
    }

    public synchronized void removeProductChecked(Product product, boolean needNotify) {
        if (product != null) {
            mCheckedProudctIdList.remove(product.getId());
            SQLite.delete(ProductSelectInfo.class)
                    .where(ProductSelectInfo_Table.id.eq(product.getId()))
                    .execute();
            if (needNotify) {
                notifyCheckChanged();
            }
        }
    }

    public synchronized void removeAllProductChecked() {
        mCheckedProudctIdList.clear();
        notifyCheckChanged();
        SQLite.delete(ProductSelectInfo.class).execute();
    }

    private void notifyCheckChanged() {
        int count = mCheckedProudctIdList.size();
        for (OnProductCheckChangeListenner item : mOnProductCheckChangeListenners) {
            item.OnProductCheckChanged(count);
        }
    }

    public void addProductCheckChangeListener(OnProductCheckChangeListenner listenner) {
        mOnProductCheckChangeListenners.add(listenner);
    }

    public void removeProductCheckChangeListener(OnProductCheckChangeListenner listenner) {
        mOnProductCheckChangeListenners.remove(listenner);
    }

    public interface OnProductCheckChangeListenner {
        public void OnProductCheckChanged(int count);
    }

    public void clear() {
        if (mOnProductCheckChangeListenners != null) {
            mOnProductCheckChangeListenners.clear();
        }
//        if (mCheckedProudctIdList != null) {
//            mCheckedProudctIdList.clear();
//        }
//        if (mCheckedProudctIdList != null) {
//            mCheckedProudctIdList.clear();
//        }
    }

}
